package edu.nf.api.controller.item;

import edu.nf.api.controller.BaseController;
import edu.nf.api.fileutil.FileUploadUtil;
import edu.nf.waimai.dao.item.ItemDAO;
import edu.nf.waimai.dao.item.impl.ItemDAOImpl;
import edu.nf.waimai.model.ltem.Item;
import edu.nf.waimai.service.item.ItemService;
import edu.nf.waimai.service.item.impl.ItemServiceImpl;
import org.nf.web.annotation.Model;
import org.nf.web.annotation.RequestMapping;
import org.nf.web.annotation.RequestParam;
import org.nf.web.servlet.View;
import org.nf.web.servlet.view.JsonView;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.*;

public class ItemController extends BaseController {
    private ItemService service = new ItemServiceImpl();

    @RequestMapping("/item/listItemsByShopId")
    public View listItemsByShopId(@RequestParam("shopId") int shopId){
        return new JsonView(success(service.listItemsByShopId(shopId)));
    }

    /**
     * 添加商品
     *
     * @param item
     * @return
     */
    @RequestMapping("/item/add")
    public View addItem(@Model Item item) {
        return new JsonView(success(service.addItem(item)));
    }

    /**
     * 删除商品
     *
     * @param itemId
     * @return
     */
    @RequestMapping("/item/delItem")
    public View delItem(@RequestParam("itemId") int itemId) {
        return new JsonView(success(service.delItemById(itemId)));
    }

    /**
     * 修改商品
     *
     * @param item
     * @return
     */
    @RequestMapping("/item/updateItem")
    public View updateItem(@Model Item item) {
        return new JsonView(success(service.updateItem(item)));
    }

    /**
     * 修改商品状态
     *
     * @param itemId
     * @param state
     * @return
     */
    @RequestMapping("/item/updateItemState")
    public View updateItemStateById(@RequestParam("itemId") int itemId, @RequestParam("itemState") int state) {
        return new JsonView(success(service.updateItemStateById(itemId, state)));
    }

    /**
     * 返回全部商品
     *
     * @return
     */
    @RequestMapping("/item/getItemList")
    public View listItem() {
        return new JsonView(success(service.listItem()));
    }

    /**
     * 通过状态查询全部商品
     *
     * @param itemState
     * @return
     */
    @RequestMapping("/item/getItemListState")
    public View listItemByState(@RequestParam("itemState") int itemState) {
        return new JsonView(success(service.listItemByState(itemState)));
    }

    /**
     * 通过类型id和商家id查询全部商品
     *
     * @param itemTypeId
     * @return
     */
    @RequestMapping("/item/getItemListByItemTypeIdAndShopId")
    public View listItemByItemTypeId(@RequestParam("itemTypeId") int itemTypeId,@RequestParam("shopId") int shopId) {
        return new JsonView(success(service.listItemByItemTypeIdAndShopId(itemTypeId,shopId)));
    }

    @RequestMapping("/item/uploadImg")
    public View uploadImg(Part[] file, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String filename = "";
        for (Part part1 : file) {
            //获取路径
            String filePath = request.getServletContext().getRealPath("/images/item");
            //创建工具类
            FileUploadUtil fileUploadUtil = new FileUploadUtil();
            System.out.println(filePath);
            //设置文件大小
            fileUploadUtil.setMaxSize(1 * 1024 * 1024);
            //设置文件的后缀
            fileUploadUtil.setAllowFileExt("jpg,png,jpeg");
            //文件名称
            filename = fileUploadUtil.upload(part1, filePath);
        }
        return new JsonView(success(filename));
    }

    /**
     * 根据类型id查询商品
     *
     * @return
     */
    @RequestMapping("/item/getItemListItemType")
    public View listItemByTypeId(@RequestParam("itemTypeId") int itemTypeId) {
        ItemDAO dao = new ItemDAOImpl();
        return new JsonView(success(dao.listItemByTypeId(itemTypeId)));
    }
}
